<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\Cate as CateModel;
use think\Validate;
use think\Db;
use app\admin\model\Article as ArticleModel;
class Article  extends Common
{
	public function lst()
    {
        //$articleres=$list = Db::name('article')->paginate(5);
        $articleres=db('article')->field('a.*,b.catename')->alias('a')->join('bk_cate b','a.cateid=b.id')->order('id desc')->paginate(8);
        $this->assign('articleres',$articleres);
        return view('list');
    }

    public function add()
    {
        if(request()->isPost()){
            $data=input('post.');
            //dump($data);die;
            //方法1控制器层
            /*if($_FILES['thumb']['tmp_name']){
                // 获取表单上传文件 例如上传了001.jpg
                $file = request()->file('thumb');
                // 验证后移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->validate(['size'=>2097152,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                 $thumb='http://localhost/bike/'. 'public' . DS . 'uploads'.'/'.$info->getSaveName();
                 $data['thumb']=$thumb;
                }else{
                    // 上传失败获取错误信息
                    return  $file->getError();
                }
            }*/
             $validate = \think\Loader::validate('Article');//实例化link验证器
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
           
            $data['time']=time();
           //dump($data);die;
            $article=new ArticleModel();
            if($article->save($data)){
                $this->success('添加文章成功！',url('article/lst'));
            }else{
                $this->error('添加文章失败！');
            }
            return;
        }
        $cate=new CateModel();
        $catelist=$cate->cateTree();
        $this->assign('catelist',$catelist);
        return view();
    }

    public function edit($id)
    {
        if(request()->isPost()){
            $data=input('post.');

            $validate = \think\Loader::validate('Article');//实例化link验证器
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }

            $article=new ArticleModel();
            $save=$article->update($data);
            if($save){
                  $this->success('修改文章成功！',url('article/lst'));
            }else{
                $this->error('修改文章失败！');
            }
            return;
        }

        $data=db('article')->find(input('id'));
        $cate=new CateModel();
        $catelist=$cate->cateTree();
        $this->assign(array(
            'catelist'=>$catelist,
            'data'=>$data,
            ));
        return view();
    }

    public function delete(){
         
           
        if(ArticleModel::destroy(input('id'))){
                  $this->success('删除文章成功！',url('article/lst'));
            }else{
                $this->error('删除文章失败！');
            }
    }



















}
